home *** CD-ROM | disk | FTP | other *** search
- Path: news.pi.net!news
- From: guyver@pi.net (M.L.A. Lammerse)
- Newsgroups: comp.lang.c
- Subject: Re: passing 2D arrays to functions
- Date: Sat, 20 Apr 1996 08:25:31 GMT
- Organization: Planet Internet
- Message-ID: <4la7g9$6bg@neptunus.pi.net>
- References: <4l58sk$l6r@harbinger.cc.monash.edu.au>
- NNTP-Posting-Host: asd21.pi.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- pdrod1@mdw084.cc.monash.edu.au (Mr Paul Rodger) wrote:
-
- >Greets -
-
- >I need to pass a 2-dimensional array to a function. Obviously I just want
- >to pass a pointer to the first element, and in the past when I wanted to
- >pass a one-dimensional array to a function I did the following which should
- >work fine:
-
-
- Well, the way I do it is like this:
-
- Here is an example function that requires a 2D-array of integers ( m
- rows times n columns), it sum all the elements in it and returns that
- value:
-
- long Sum (int **Array, int MaxRows, int MaxColumns)
- {
- int i,j;
- long sum=0;
-
- for (j=0;j<MaxRows;j++)
- for (i=0;i<MaxColumns;i++)
- sum += Array[j][i];
- return sum;
- }
-
- And now to define the array, you'll create an array of pointers to an
- array of integers:
-
- #include <stdio.h>
-
- main()
- {
- int a1 = {1,2,3};
- int a2 = {4,5,6};
- int a3 = {7,8,9};
-
- int *A = { a1,
- a2,
- a3};
-
- printf ("Sum of elements : %ld \n",sum(A,3,3));
-
- }
-
- I don't know an easier way to do it (please let me know if you do).
-
- Another example of 2D-arrays as parameters is the program parameters
- in:
-
- void main(int argc, char *argv[])
-
- I don't remember under which condition you can use 'type *variable[]'
- , but it is possible.
-
- I hope this helps,
-
- Marcel
-
-
-
- ***************************************************************
- * M.L.A. Lammerse | e-mail: guyver@pi.net *
- * Electrical engineering student | phone : (31)0223-612763 *
- * at Alkmaar polytechnic in Holland | *
- ***************************************************************
-
-